package com.yanchangapp.ui.activity._n_manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.FinishPlanRequest;
import com.yanchangapp.ui.presenter.FinishPlanAtPresenter;
import com.yanchangapp.ui.view.FinishPlanAtView;
import com.zxl.baselib.baseapp.AppManager;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * page: 完成心得
 */
public class FinishPlanActivity extends BaseActivity<FinishPlanAtView, FinishPlanAtPresenter> implements FinishPlanAtView {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.etKeyInput)
    EditText etKeyInput;
    @BindView(R.id.btnEnsure)
    Button btnEnsure;
    private static final String PLAN_ID = "PLAN_ID";
    private static final String IS_FROM_DETAIL = "IS_FROM_DETAIL";
    private long planID;
    private boolean isFromDetail;

    public static void show(Activity activity, long planID) {
        Intent intent = new Intent(activity, FinishPlanActivity.class);
        intent.putExtra(PLAN_ID, planID);
        activity.startActivity(intent);
    }

    public static void show(Activity activity, long planID,boolean isFromDetail) {
        Intent intent = new Intent(activity, FinishPlanActivity.class);
        intent.putExtra(PLAN_ID, planID);
        intent.putExtra(IS_FROM_DETAIL,isFromDetail);
        activity.startActivityForResult(intent,AppConstant.RECODE_FROM_HISTOTY_DETAIL);
    }

    @Override
    protected void init() {
        planID = getIntent().getLongExtra(PLAN_ID, -1);
        isFromDetail = getIntent().getBooleanExtra(IS_FROM_DETAIL,false);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_finish_plan;
    }

    @Override
    protected FinishPlanAtPresenter createPresenter() {
        return new FinishPlanAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("完成心得");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String conclusion =  etKeyInput.getText().toString();
                FinishPlanRequest finishPlanRequest = new FinishPlanRequest();
                finishPlanRequest.setConclusion(conclusion);
                mPresenter.actionFinishPlan(planID,finishPlanRequest);
            }
        });
    }

    @Override
    public void finishPlanSuccess(String success) {
        //UIUtils.showToast(success);
        if (isFromDetail){
            // 返回
            Intent intent = new Intent();
            intent.putExtra("data",etKeyInput.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
        }else {
            mRxManager.post(DailyPlanActivity.CMD_START_WORK,true);
            AppManager.getAppManager().returnToActivity(DailyPlanActivity.class);
        }
    }

    @Override
    public void finishPlanFailure(String err) {
        UIUtils.showToast(err);
    }


}
